<?php
/**
 * Created by PhpStorm.
 * User: wanghongbing
 * Date: 2018/11/15
 * Time: 上午9:38
 */
namespace app\Admin\model;
use think\captcha\Captcha;
use think\Model;
use think\Session;

class Admin extends Model
{
    static public function Login($username,$password,$code){

        //验证用户名是否存在
        $map = array('username' =>$username);

        $Users = self::get($map);
        //$Users要么对象，要么空

        if (!is_null($Users)){

            if($Users -> checkpassword($password)){

                //登录
             $captcha =  new Captcha();
             if ($captcha -> check($code)){
                 session('user',$username);

                 return true;

             } return false;

            }
        }
        return false;

    }

    public function checkpassword($password){

        if ($this -> getData('password') ===$this::encryptPassword($password)){

            return true;
        }else
        {
            return false;
        }

    }
    public function encryptPassword($password){

        return sha1(md5($password).'mengyunzhi');

    }

    static public function logout(){

        //注销

        session('user',null);

        return true;
    }

}